package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.ItemDescMapper;
import com.jt.pojo.Item;
import com.jt.pojo.ItemDesc;
import com.jt.vo.EasyUITable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jt.mapper.ItemMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private ItemDescMapper itemDescMapper;

    /*
    @Override
    public EasyUITable findItemByPage(Integer page, Integer rows) {
        //计算起始位置
        int startIndex = (page - 1) * rows;
        List<Item> itemList = itemMapper.findItemByPage(startIndex, rows);
        //获取总记录数
        Integer total = itemMapper.selectCount(null);
        return new EasyUITable(total, itemList);
    }*/

    @Override
    public EasyUITable findItemByPage(Integer page, Integer rows) {
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("updated");
        //当用户将参数传递后,MP会自动执行分页操作,将需要的数据进行封装
        //定义分页对象
        IPage<Item> iPage = new Page<>(page, rows);
        iPage = itemMapper.selectPage(iPage, queryWrapper);
        //获取目录总数
        int total = (int) iPage.getTotal();
        //获取分页结果
        List<Item> records = iPage.getRecords();
        return new EasyUITable(total, records);
    }

    @Transactional //控制事务
    @Override
    public void saveItem(Item item, ItemDesc itemDesc) {
        //1.设置商品为上架状态
        //item.setStatus(1).setCreated(new Date()).setUpdated(new Date());
        item.setStatus(1);
        itemMapper.insert(item);//先入库才会有主键,将主键动态的返回
        //MP支持,用户的操作可以实现自动的主键回显功能
        //2.完成商品详情入库操作  要求item的id应该和itemDesc的id值相等!!!
        itemDesc.setItemId(item.getId());
        itemDescMapper.insert(itemDesc);

    }

    @Transactional
    @Override
    public void updateItem(Item item, ItemDesc itemDesc) {
        //item.setUpdated(new Date());
        itemMapper.updateById(item);//根据id进行数据更新,在item-edit中隐藏了	<input type="hidden" name="id"/>
        itemDesc.setItemId(item.getId());
        itemDescMapper.updateById(itemDesc);
    }

    @Transactional
    @Override
    public void deleteItems(Long[] ids, ItemDesc itemDesc) {
        //1.将数组转换为list集合
       /* List<Long> idList = Arrays.asList(ids);
        itemMapper.deleteBatchIds(idList);*/
        //2.手写sql
        itemMapper.deleteItems(ids);
        List<Long> idList = Arrays.asList(ids);
        itemDescMapper.deleteBatchIds(idList);

    }

    @Transactional
    @Override
    public void updateStatus(Integer status, Long[] ids) {
        //手写SQL
        itemMapper.updateStatus(ids, status);

        //利用MP
        Item item = new Item();
        item.setStatus(status);
        //定义修改操作的条件构造器
        UpdateWrapper<Item> updateWrapper = new UpdateWrapper<>();
        List<Long> idList = Arrays.asList(ids);//数据转化为集合
        updateWrapper.in("id", idList);
        //根据mp机制.实现批量的数据更新操作
        itemMapper.update(item, updateWrapper);

    }

    @Override
    public ItemDesc findItemDescById(Long itemId) {
        return itemDescMapper.selectById(itemId);
    }
}
